//
//  TransitionAnimation.m
//  CustomTransition
//
//  Created by Xwoder on 17/1/31.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "TransitionAnimation.h"

static NSTimeInterval const duration = 2;

@implementation TransitionAnimation

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return duration;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    CGRect fromViewControllerFrame = [transitionContext initialFrameForViewController:fromViewController];
    CGRect toViewControllerInitFrame = CGRectMake(0, fromViewControllerFrame.size.height, fromViewControllerFrame.size.width, fromViewControllerFrame.size.height);
    
    UIView *fromViewControllerView = fromViewController.view;
    UIView *toViewControllerView = toViewController.view;
    
    fromViewControllerView.frame = fromViewControllerFrame;
    toViewControllerView.frame = toViewControllerInitFrame;
    
    [transitionContext.containerView addSubview:fromViewControllerView];
    [transitionContext.containerView addSubview:toViewControllerView];
    
    [UIView animateWithDuration:duration animations:^{
        toViewControllerView.frame = fromViewControllerFrame;
        toViewControllerView.alpha = 1;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

@end
